package cn.com.nou.base.service.bdm;

import cn.com.nou.base.dao.bdm.CourseCategoryMapper;
import cn.com.nou.base.dao.bdm.LevelMapper;
import cn.com.nou.common.BaseService;
import cn.com.nou.common.builder.PaginationBuilder;
import cn.com.nou.common.entity.NouEnumerationT;
import cn.com.nou.common.entity.Pagination;
import cn.com.nou.common.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class LevelService extends BaseService {
    @Autowired
    LevelMapper levelMapper;

    public Pagination getLevelList(NouEnumerationT nouEnumerationT, int pageNo, int pageSize) {
        PaginationBuilder builder = new PaginationBuilder();
        Pagination page = builder.getPaginating(pageNo, pageSize);
        List<NouEnumerationT> courseCategoryList = levelMapper.getLevelList(nouEnumerationT, page.getOffset(), page.getLimit());
        page.setResultList(courseCategoryList);
        page.setTotal(levelMapper.getLevelCount(nouEnumerationT.getType()));
        page.setPageSize(pageSize);
        page.setPageNo(pageNo);
        page.setOffset(page.getOffset());
        page.setLimit(page.getLimit());
        return page;
    }

    public int addLevel(NouEnumerationT nouEnumerationT) {
        if (levelMapper.getSameLevelItemCodeOrItemName(nouEnumerationT).size() == 0) {
            nouEnumerationT.setId(Utils.getUUID());
            return levelMapper.addLevel(nouEnumerationT);
        } else {
            return 2;
        }
    }

    public int updateLevel(NouEnumerationT nouEnumerationT) {
        return levelMapper.updateLevel(nouEnumerationT);
    }

    public List<Integer> deleteLevel(NouEnumerationT nouEnumerationT) {
        List<Integer> codes = new ArrayList();
        if (levelMapper.getStudentUsedCount(nouEnumerationT) > 0) {
            codes.add(1);
        }
        if (levelMapper.getScholUsedCount(nouEnumerationT) > 0) {
            codes.add(2);
        }
        if (levelMapper.getSpecUsedCount(nouEnumerationT) > 0) {
            codes.add(3);
        }
        if (codes.size() == 0 && levelMapper.deleteLevel(nouEnumerationT.getId()) > 0) {
            codes.add(4);
        }
        return codes;
    }

}
